﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Xml;
using System.Workflow.Runtime;
using Openfree.Framework.DataAccess;

namespace Openfree.Workflow.Common
{
	public class WorkflowAccess
	{
        private static string wfConnection = null;
        private static string sysConnection = null;

        private static void LoadConfig()
        {
            string webConfig = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            webConfig += "web.config";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(webConfig);

            //xpath 大小写敏感
            wfConnection = xmlDoc.SelectSingleNode("/configuration/appSettings/add[@key='WFDSN']").Attributes["value"].Value;
            sysConnection = xmlDoc.SelectSingleNode("/configuration/appSettings/add[@key='SYSDSN']").Attributes["value"].Value;
        }

        
        public static string WFConnection
        {
            get
            {
                if(wfConnection == null) LoadConfig();
                return wfConnection;
            }
        }


        public static string SYSConnection
        {
            get
            {
                if(sysConnection == null) LoadConfig();
                return sysConnection;
            }
        }

        /// <summary>
        /// 设置工作流状态，比如：Running，Complete
        /// </summary>
        /// <param name="instanceId"></param>
        /// <param name="workflowStatus"></param>
        /// <returns></returns>
        public static void SetWorkflowState(Guid instanceId, WorkflowStatus workflowStatus)
        {
            string cmdText = "update T_Workflow set CurrentState='{0}' where InstanceID='{1}'";
            cmdText = string.Format(cmdText, workflowStatus, instanceId.ToString());
            BaseAccess.ExecuteNonQuery(SYSConnection, cmdText);
        }


        /// <summary>
        /// 翻译工作流状态，比如：Running，Complete
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetWorkfolwStateName(Object state)
        {
            string result = string.Empty;
            if (state == null)
            {
                result = string.Empty;
            }
            else if ("Created".Equals(state.ToString()))
            {
                result = "创建";
            }
            else if ("Running".Equals(state.ToString()))
            {
                result = "运行";
            }
            else if ("Suspended".Equals(state.ToString()))
            {
                result = "暂停";
            }
            else if ("Terminated".Equals(state.ToString()))
            {
                result = "异常";
            }
            else if ("Completed".Equals(state.ToString()))
            {
                result = "完成";
            }

            return result;
        }


        public static DataSet LoadWorkflowImage()
        {
            return BaseAccess.GetDataSet(SYSConnection, "select * from T_Thumbnail where Category='WorkflowCategory'");
        }
	}

    public class BaseAccess : SqlHelper
    {

    }

    public class SecurityAccess : SqlSecurity
    {
        public SecurityAccess(SqlConnection conn)
            : base(conn)
        {

        }
    }
}
